<?php

namespace app\adminabc\controller;

use app\adminabc\controller\Base;

class Website extends Base {
	public function profile() {
		if ($this->request->isPost ()) {
			$data = $_POST;
			model ( 'Website' )->delAll ( array ([ 'key','not in','1' ] ) );

            $cache = [];
			foreach ( $data as $k => $v ) {
				$key .= $k . ',';
				$dt ['val'] = $v;

                //缓存订单提醒配置
                if($k == 'order_notice_status' || $k == 'order_notice_times' || $k == 'order_notice_interval' || $k == 'ajax_interval'){
                    $cache[$k] =  $v;
                }

				$info = model ( 'Website' )->getInfo ( array ([ 'key','=',$k ] ) );
				if (count ( $info ) == 0) {
					$dt ['key'] = $k;
					model ( 'Website' )->add ( $dt );
				} else
					model ( 'Website' )->edit ( array ([ 'key','=',$k ] ), $dt );
			}
            cache('order_notice_config', $cache);
			// $key = rtrim ( $key, ',' );
			// model ( 'Website' )->delAll ( array ([ 'key','not in',$key ] ) );
			$this->success ( '保存成功' );
		} else {
			$info = model ( 'Website' )->getAll ();
			$this->assign ( 'info', $info );
			return $this->fetch ();
		}
	}
	function nav() {
		$data = model ( 'Website' )->getAllNav ();
		$this->assign ( 'list', $data );
		return $this->fetch ();
	}
	function addnav() {
		if ($this->request->isPost ()) {
			$data ['reid'] = $this->request->param ( 'reid' ) == '' ? 0 : $this->request->param ( 'reid' );
			$data ['sort_id'] = $this->request->param ( 'sort_id' );
			$data ['title'] = $this->request->param ( 'title' );
			$data ['status'] = $this->request->param ( 'status' ) == '' ? 1 : $this->request->param ( 'status' );
			$data ['url'] = $this->request->param ( 'url' );
			$data ['create_time'] = time ();
			$data ['channeltype'] = $this->request->param ( 'channeltype' );
			$data ['user_id'] = $this->admin_id;
			$log = model ( 'Website' )->addNav ( $data );
			if ($log > 0)
				$this->success ( '添加成功' );
			else
				$this->error ( '添加失败' );
		} else {
			$Alllist = model ( 'Website' )->getAllNav ();
			$tree = new \CateTree ();
			$treeArr = $tree->tree ( $Alllist, 'title', 'id', 'reid' );
			$this->assign ( 'tree', $treeArr );
			return $this->fetch ( 'add-nav' );
		}
	}
	function editnav() {
		$id = $this->request->param ( 'id' );
		$info = model ( 'Website' )->getNavInfo ( array ([ 'id','=',$id ] ) );
		if ($this->request->isPost ()) {
			$data ['reid'] = $this->request->param ( 'reid' ) == '' ? 0 : $this->request->param ( 'reid' );
			$data ['sort_id'] = $this->request->param ( 'sort_id' );
			$data ['title'] = $this->request->param ( 'title' );
			$data ['status'] = $this->request->param ( 'status' ) == '' ? 1 : $this->request->param ( 'status' );
			$data ['url'] = $this->request->param ( 'url' );
			$data ['create_time'] = time ();
			$data ['channeltype'] = $this->request->param ( 'channeltype' );
			$log = model ( 'Website' )->editNav ( array ([ 'id','=',$id ] ), $data );
			if ($log > 0)
				$this->success ( '更新成功' );
			else
				$this->error ( '更新失败' );
		} else {
			$Alllist = model ( 'Website' )->getAllNav ();
			$tree = new \CateTree ();
			$treeArr = $tree->tree ( $Alllist, 'title', 'id', 'reid' );
			$this->assign ( 'tree', $treeArr );
			$this->assign ( 'info', $info );
			return $this->fetch ( 'add-nav' );
		}
	}
	function delnav() {
		$id = $this->request->param ( 'id' );
		$log = model ( 'Website' )->delNav ( array ([ 'id','=',$id ] ) );
		if ($log > 0)
			echo json_encode ( array ('code' => 0,'msg' => '删除成功' ) );
		else
			echo json_encode ( array ('code' => 999,'msg' => $id ) );
	}
	function ipforbid() {
		$data = model ( 'Website' )->getIpforbid ();
		$this->assign ( 'list', $data ['list'] );
		$this->assign ( 'page', $data ['page'] );
		return $this->fetch ();
	}
	function delIpforbid() {
		$id = $this->request->param ( 'id' );
		$map [] = [ 'id','=',$id ];
		$log = model ( 'Website' )->delIpforbid ( $map );
		if ($log > 0)
			echo json_encode ( array ('status' => 'yes','msg' => '删除成功' ) );
		else
			echo json_encode ( array ('status' => 'no','msg' => '删除失败' ) );
	}

    public function get_order_notice_config()
    {
        $data = cache('order_notice_config');

        if (empty($data)){
            $tmp = [];
            foreach (['order_notice_times','order_notice_status','order_notice_interval','ajax_interval']as $v){
                $info = model ( 'Website' )->getInfo ( array ([ 'key','=',$v ] ) );
               $tmp[$v] = $info;
            }
            $data = $tmp;
            cache('order_notice_config',$tmp);
        }

        $notice_order = 0;
        $notice_withdraw = 0;

        //查询是否有新的订单

        $new_order = cache('new_order');

        $new_withdraw = cache('new_withdraw');

        if (!empty($new_order)){
            $notice_order = 1;
         }

        if (!empty($new_withdraw)){
            $notice_withdraw = 1;
         }

        echo json_encode(['msg'=>'order_notice_config 成功!','data'=>$data,'notice_withdraw' => $notice_withdraw,'notice_order' => $notice_order]);
    }
}